#!/usr/bin/env python
# -*- coding:utf-8 -*-

from typing import List, Optional

from libs.torndb import Connection


license_names = ['TBNZ', 'TBSG']


class TransferOutConfig(object):
    @classmethod
    def query_list(cls, db: Connection,
                   market: str,
                   language: str = "zh_CN",
                   status: Optional = None,
                   tiger_executing_broker: Optional = None,
                   company_name: Optional = None,
                   license_name: Optional = None) -> List:
        sql = '''
        SELECT c.id,
               name,
               t.help_doc_link 
        FROM transfer_out_config c
                JOIN transfer_name n ON n.config_id = c.id
                JOIN supported_transfer_license l ON l.config_id = c.id
                LEFT JOIN transfer_executing_config t ON c.id = t.transfer_id 
        WHERE c.market = '{}'
           AND n.language = '{}'
            '''.format(market, language)
        if status is not None:
            sql += f"AND status={status} "
        if license_name is not None and license_name in license_names:
            sql += f"AND {license_name}<>0 "
        sql += "ORDER BY c.order; "
        rows = db.query(sql)
        return
